package gspushmq.v1.subscribe;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.java_websocket.WebSocket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import gspushmq.model.ApiDataMessage;
import gspushmq.model.ApiMessage;
import gspushmq.model.ApiMessageType;
import gspushmq.model.Data;
import gspushmq.model.Message;
import gspushmq.util.SynUtil;
import gspushmq.v1.save.SaveMassRecord;
import gspushmq.v1.save.SaveSendRecord;
import gspushmq.v1.subscribe.send.core.GGMessageSend;

/**
 *
 * 处理第一次订阅
 * @version 1.0
 * @since JDK1.7
 * @author yutao
 * @company 上海朝阳永续信息技术有限公司
 * @copyright (c) 2016 SunTime Co'Ltd Inc.All rights reserved.
 * @date 2018年11月21日上午10:58:34
 */
public class HandlerFirstSubscribe {



	@SuppressWarnings("rawtypes")
	public static void done(ApiMessage apiMess, WebSocket wst) {
		//消息子类型
		String mqSubType = apiMess.getMq_sub_type();
		String accountId = apiMess.getAccount_id();

		Map<String, String> paramsMap = new HashMap<>();
		paramsMap.put("account_ids", apiMess.getAccount_id());
		JSONObject jsonObject = GGMessageSend.commonUrl(paramsMap, apiMess.getApi_url());
		if(jsonObject == null) {
			return;
		}
		
		Object dataObj = jsonObject.get("data");
		
		if(dataObj == null) {
			return;
		}
		
		if(!(dataObj instanceof Map)) {
			return;
		}
		
		Map jsonObj = (Map)dataObj;
		Object objList = jsonObj.get(accountId);
		
		ApiMessageType apiType = new ApiMessageType();
		apiType.setApi_url(apiMess.getApi_url());
		apiType.setMq_sub_type(apiMess.getMq_sub_type());
		apiType.setReqeust_id(apiMess.getRequest_id());
		
		/*DataBase dataBase = GGDataStorage.ApiBaseMap.get(apiType);
		if(dataBase == null) {
			dataBase = new DataBase();
			GGDataStorage.ApiBaseMap.put(apiType, dataBase);
		}
		ConcurrentHashMap<String, Object> baseAidMap = dataBase.getAccountIdsMap();
		if(baseAidMap == null) {
			baseAidMap = new ConcurrentHashMap<>();
			dataBase.setAccountIdsMap(baseAidMap);
		}*/
		
		Map<String, Object> baseAidMap = SynUtil.synApiCommon(apiType);
		
		String messageFlag = apiType.getMessageFlag();
		String baseKey = accountId + "|" + messageFlag;
		baseAidMap.put(baseKey, objList);
		Object pushObj = objList;
		if("count".equals(mqSubType)) {
			pushObj = ((List)pushObj).size();
		}
		
		//将处理好的数据记录到生产表
		ApiDataMessage apiDataMessage = handlerData(apiMess, pushObj, "第一次订阅");
		sendWebSocket(apiMess, wst, apiDataMessage);
	}


	/**
	 * 写日志
	 * @param message
	 * @param data
	 * @author yutao
	 * @date 2019年1月11日下午1:53:31
	 */
	private static void writeLog(Message message, Data data) {
		SaveSendRecord.save(message, data);
	}
	
	/**
	 * 推送消息给客户端
	 * @param ams
	 * @param wst
	 * @param apiDataMessage
	 * @author yutao
	 * @date 2018年11月16日下午3:47:01
	 */
	public static void sendWebSocket(Message ams, WebSocket wst, Data apiDataMessage) {
		//定位到用户订阅了这类消息
		Map<String, Object> result = new HashMap<>();
		//表示有效
		result.put("code", 0);
		result.put("request_id", ams.getRequest_id());
		result.put("uuid", apiDataMessage.getUuid());
		result.put("data", apiDataMessage.getContent());
		wst.send(JSON.toJSONString(result));
		//写日志
		writeLog(ams, apiDataMessage);
	}

	
	/**
	 * 封装生产好的数据并写入日志
	 * @param apiMess
	 * @param pushList
	 * @author yutao
	 * @param
	 * @return 
	 * @date 2018年11月16日下午3:53:04
	 */
	public static ApiDataMessage handlerData(ApiMessage apiMess, Object pushList, String title) {
		ApiDataMessage apiDataMessage = new ApiDataMessage();
		apiDataMessage.setUserType(apiMess.getUser_type());
		apiDataMessage.setApiUrl(apiMess.getApi_url());
		apiDataMessage.setParams(apiMess.getParams());
		apiDataMessage.setMqType(apiMess.getMq_type());
		apiDataMessage.setMqSubType(apiMess.getMq_sub_type());
		apiDataMessage.setTitle(title);
		apiDataMessage.setContent(pushList);
		SaveMassRecord.save(apiDataMessage);
		return apiDataMessage;
	}

}
